<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;
use think\facade\View;

use app\common\logic\ModuleLogic;
use think\facade\Db;
use think\facade\Cache;

use app\admin\validate\SystemMenu as validateSystemMenu;

use app\common\model\SystemMenu as SystemMenuModel;

class System extends Base
{
    function right_list(){
        $type = input('type',0);
        $moduleLogic = new ModuleLogic;
        if (!$moduleLogic->isModuleExist($type)) {
            $this->error('权限类型不存在');
        }
        $modules = $moduleLogic->getModules();
        $group = $moduleLogic->getPrivilege($type);

        $condition = [];
        $name = input('name');
        if(!empty($name)){
            $condition['name|right'] = array('like',"%$name%");
        }
        $right_list = Db::name('system_menu')->where($condition)->order('sort asc,id asc')->select()->toArray();
        foreach($right_list as $k=>$v){
            $right_list[$k]['show_name'] = $v['show'] == 1 ?'显示':'不显示';
        }
        $SystemMenuModel = new SystemMenuModel();
        $right_list = $SystemMenuModel->menulist($right_list,0,0);

        View::assign('right_list',$right_list);
        View::assign('group',$group);
        View::assign('modules',$modules);
        return View::fetch();
    }

    public function right_add(){
        if(IS_POST){
            $param = input('param.');

            $validate = new validateSystemMenu;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $SystemMenuModel = new SystemMenuModel();
            $res = $SystemMenuModel->handle($param);
            return dyajaxReturn($res['code'],$res['msg']);
        }else {
            $pid = input('param.pid');
            View::assign('pid',$pid);

            //父节点
            $SystemMenuModel = new SystemMenuModel();
            $sys_list = $SystemMenuModel -> where('is_del','=',0)->order('id asc')->select();
            $tree = $SystemMenuModel->menulist($sys_list,0,0);
            View::assign('tree',$tree);

            //选择权限码
            $planPath = APP_PATH.MODULE_NAME.'/controller';;
            $dirRes   = opendir($planPath);
            $planList = array();
            while($dir = readdir($dirRes))
            {
                if(!in_array($dir,array('.','..','.svn')))
                {
                    $planList[] = basename($dir,'.php');
                }
            }
            sort($planList);
            View::assign('planList',$planList);
            return View::fetch();
        }
    }


    public function right_edit(){
        $type = input('type',0);
        $moduleLogic = new ModuleLogic;
        
        if (!$moduleLogic->isModuleExist($type)) {
            $this->error('模块不存在或不可见');
        }
        
        if(IS_POST){
            $param = input('param.');
            $validate = new validateSystemMenu;
            $result = $validate->scene('edit')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $SystemMenuModel = new SystemMenuModel();
            $res = $SystemMenuModel->handle($param);
            return dyajaxReturn($res['code'],$res['msg']);
        }else{
            $SystemMenuModel = new SystemMenuModel();
            $id = input('id');
            if($id){
                $info = $SystemMenuModel->where(array('id'=>$id))->find();
                if($info['right']) {
                    $right = explode(',', $info['right']);
                    $new_right = [];
                    foreach ($right as $k => $v) {
                        $new_right[$k]['right'] = $v;
                        $new_right[$k]['IdStr'] = getIdStr($v);
                    }
                    $info['right'] = $new_right;
                }
                View::assign('info',$info);
            }

            //父节点
            $sys_list = $SystemMenuModel -> where('is_del','=',0)->order('sort asc')->select();
            $tree = $SystemMenuModel->menulist($sys_list,0,0);
            View::assign('tree',$tree);

            //选择权限码
            $planPath = APP_PATH.MODULE_NAME.'/controller';;
            $dirRes   = opendir($planPath);
            $planList = array();
            while($dir = readdir($dirRes))
            {
                if(!in_array($dir,array('.','..','.svn')))
                {
                    $planList[] = basename($dir,'.php');
                }
            }
            sort($planList);
            View::assign('planList',$planList);
            return View::fetch();
        }
    }
     
     public function right_del(){
     	$id = input('del_id');
     	if(is_array($id)){
     		$id = implode(',', $id); 
     	}
     	if(!empty($id)){
     		$r = Db::name('system_menu')->where("id in ($id)")->delete();
     		if($r){
                return dyajaxReturn(1,'删除成功');
     		}else{
                return dyajaxReturn(0,'删除失败');
     		}
     	}else{
            return dyajaxReturn(0,'参数有误');
     	}
     }

    public function delList(){
        $ids = input('post.ids','');
        empty($ids) && $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        $listIds = rtrim($ids);
        Db::name('system_menu')->whereIn('id',$listIds)->delete();
        $this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>url("System/right_list")]);
    }


    function ajax_get_action(){
        $control = input('controller');
        $id = input('id');
        $SystemMenuModel = new SystemMenuModel();
        if($id){
            $sysMenu_info = $SystemMenuModel -> where('id','=',$id)->find();
            $right_arr = explode(',',$sysMenu_info['right']);

        }

        $selectControl = [];
        $className = "app\\".MODULE_NAME."\\controller\\".$control;
        $methods = (new \ReflectionClass($className))->getMethods(\ReflectionMethod::IS_PUBLIC);
        foreach ($methods as $method) {
            if ($method->class == $className) {
                if ($method->name != '__construct' && $method->name != '_initialize') {
                    $selectControl[] = $method->name;
                }
            }
        }
        $html = '';
        foreach ($selectControl as $val){
            if($id){
                $quanx_str = $control.'@'.$val;
                if(in_array($quanx_str,$right_arr)){
                    $html .= '<input type="checkbox" value="' . $val . '" lay-skin="primary" title="' . $val . '" lay-filter="choose_act" checked="" >';
                }else{
                    $html .= '<input type="checkbox" value="' . $val . '" lay-skin="primary" title="' . $val . '" lay-filter="choose_act" >';
                }
            }else {
                $html .= '<input type="checkbox" value="' . $val . '" lay-skin="primary" title="' . $val . '" lay-filter="choose_act" >';
            }
            $data_href = 'admin/'.$control.'/'.$val;
            $html .= '<div onclick="dataHref(\''.$data_href.'\')"  ><a href="javascript:;">生成节点跳转</a></div><br/>';
        }
        exit($html);
    }
}